"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 使用alter database方式修改autocommit为off，数据库为A兼容模式库，连接数据库查询
Create At   : 2023/01/16
@zou_jialiang0501162244
Description :
    1、创建兼容A库
    2、设置兼容A库autocommit=0
    3、连接兼容A库查询autocommit
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、连接后有WARNING，查询值为on
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db1_name = 'db_autocommit_0003_1'
        self.db2_name = 'db_autocommit_0003_2'
        self.log.info("-----创建兼容B库-----")
        preset_db = f"drop database if exists {self.db1_name};" \
                    f"create database {self.db1_name} dbcompatibility 'B';"
        preset_db_res = self.pri_sh.execut_db_sql(preset_db)
        self.log.info(preset_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, preset_db_res,
                      f"创建兼容B库失败")

    def test_autocommit(self):
        text1 = '-----step1:创建兼容A数据库;expect:成功-----'
        self.log.info(text1)
        create_db = f"drop database if exists {self.db2_name};" \
                    f"create database {self.db2_name} dbcompatibility 'A';"
        createdb_res = self.pri_sh.execut_db_sql(create_db)
        self.log.info(createdb_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, createdb_res,
                      f"创建兼容A库失败")

        text2 = '-----step2:设置兼容A数据库autocommit=0;expect:成功-----'
        self.log.info(text2)
        default_res = self.pri_sh.execut_db_sql(
            f'alter database {self.db2_name} set autocommit = 0;',
            dbname=self.db1_name)
        self.log.info(default_res)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, default_res,
                      f'执行失败: {text2}')

        text3 = '-----step3:连接兼容A库查询autocommit;' \
                'expect:连接后有WARNING，查询值为on-----'
        self.log.info(text3)
        sql_res = self.pri_sh.execut_db_sql(f'show autocommit;',
                                            dbname=self.db2_name)
        self.log.info(sql_res)
        war_msg = 'WARNING:  SET AUTOCOMMIT TO OFF is no longer supported'
        self.assertIn(war_msg, sql_res, f'执行失败: {text3}')
        self.assertEqual(sql_res.splitlines()[-2].strip(), 'on',
                         f'执行失败: {text3}')

    def tearDown(self):
        text4 = '-----step4:清理环境; expect:成功-----'
        self.log.info(text4)
        create_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db1_name};"
            f"drop database if exists {self.db2_name};")
        self.log.info(create_db_res)
        self.assertEqual(
            create_db_res.count(self.constant.DROP_DATABASE_SUCCESS), 2,
            f'删库失败: {text4}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
